//
//  UserInfoModel.m
//  Assembly
//
//  Created by 葛悦彣 on 2023/11/23.
//

#import "UserInfoModel.h"
#import "AppDelegate.h"
#import "SCLoginViewController.h"
#import "SCBaseNavigationViewController.h"


#define kAccountCacheFileName @"kAccountCacheFileName"

#define kCache ([UserInfoModel KeyForGS:NSStringFromSelector(_cmd)])

@implementation UserInfoModel

@synthesize accountCache = _accountCache;

+ (instancetype)shared{
    static dispatch_once_t onceToken;
    static UserInfoModel *sharedManager;
    dispatch_once(&onceToken, ^{
        sharedManager = [[UserInfoModel alloc]init];
    });
    
    return sharedManager;
}

- (YYCache *)accountCache{
    if (!_accountCache) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docPath = [paths lastObject];
        NSString *path = [docPath stringByAppendingPathComponent:kAccountCacheFileName];
        [self moveAccountCacheTo:path];
        _accountCache = [YYCache cacheWithPath:path];
        [_accountCache.memoryCache setCostLimit:50 * 1000 * 1000];//50M
        [_accountCache.memoryCache setAgeLimit:2 * 60];
    }
    return _accountCache;
}


- (void)moveAccountCacheTo:(NSString *)aPath{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *cacheFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [cacheFolder stringByAppendingPathComponent:kAccountCacheFileName];
    
    if ([manager fileExistsAtPath:filePath]) {
        if ([manager moveItemAtPath:filePath toPath:aPath error:nil]) {
            NSLog(@"kAccountCacheFileName移动成功");
        } else {
            NSLog(@"kAccountCacheFileName移动失败");
            if ([manager removeItemAtPath:filePath error:nil]) {
                NSLog(@"kAccountCacheFileName删除成功");
            }else{
                NSLog(@"kAccountCacheFileName删除失败");
            }
        }
    }
}

-(NSString *)username{
    return (NSString *)[self.accountCache objectForKey:kCache];
}
-(void)setUsername:(NSString *)username{
    [self.accountCache setObject:username forKey:kCache];
}
-(NSString *)realname{
    return (NSString *)[self.accountCache objectForKey:kCache];
}
-(void)setRealname:(NSString *)realname{
    [self.accountCache setObject:realname forKey:kCache];
}
-(NSString *)phone{
    return (NSString *)[self.accountCache objectForKey:kCache];
}
-(void)setPhone:(NSString *)phone{
    [self.accountCache setObject:phone forKey:kCache];
}
-(NSString *)token{
    return (NSString *)[self.accountCache objectForKey:kCache];
}
-(void)setToken:(NSString *)token{
    [self.accountCache setObject:token forKey:kCache];
}

-(NSString *)rolesStr{
    return (NSString *)[self.accountCache objectForKey:kCache];
}
-(void)setRolesStr:(NSString *)rolesStr{
    [self.accountCache setObject:rolesStr forKey:kCache];
}
- (NSString *)OrgCode{
    return (NSString *)[self.accountCache objectForKey:kCache];
}

-(void)setOrgCode:(NSString *)OrgCode{
    [self.accountCache setObject:OrgCode forKey:kCache];
}

-(int)userIdentity{
    return  [NSString stringWithFormat:@"%@",[self.accountCache objectForKey:kCache]].intValue;
}

- (void)setUserIdentity:(int)userIdentity{
    [self.accountCache setObject:@(userIdentity) forKey:kCache];
}
- (NSString *)userID {
    return (NSString *)[self.accountCache objectForKey:kCache];

}
-(void)setUserID:(NSString *)userID{
    [self.accountCache setObject:userID forKey:kCache];
}
+ (NSString *)KeyForGS:(NSString *)str{
    return [str hasPrefix:@"set"] ? [self keyForSetter:str] : [self keyForGetter:str];
}

+ (NSString *)keyForGetter:(NSString *)str{
    return str.lowercaseString;
}

+ (NSString *)keyForSetter:(NSString *)str{
    return [[str substringToIndex:str.length - 1]substringFromIndex:@"set".length].lowercaseString;
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{
        @"roles":[UserInfoRolesModel class],
        @"userInfo":[UserInfoDeModel class],
    };
}

+ (BOOL)deleteAccount {
    [[UserInfoModel shared].accountCache removeAllObjects];//清空数据
    AppDelegate *delegate =  (AppDelegate*)[UIApplication sharedApplication].delegate;
    SCBaseNavigationViewController *page = [[SCBaseNavigationViewController alloc] initWithRootViewController: [SCLoginViewController new]];
    delegate.window.rootViewController = page;
    [[DTKTool getCurrentVC].view showWarning:@"请重新登陆"];
    return YES;
}

@end
@implementation UserInfoRolesModel
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"ID"  : @"id",
             @"Description"  : @"description",
    };
}
@end

@implementation UserInfoDeModel
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"ID"  : @"id",
    };
}
@end
